package com.zzxz.practicaltrainingsys.util;

import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class circleFileUtil {

    public static void uploadFile(byte[] file, String filePath, String fileName) throws Exception {
        File targetDir = new File(filePath);
        if (!targetDir.exists() && !targetDir.mkdirs()) {
            throw new RuntimeException("无法创建目录: " + filePath);
        }

        Path path = Paths.get(targetDir.getAbsolutePath(), fileName);
        Files.write(path, file);
    }

    public static String getUpLoadFilePath() {
        try {
            File path = new File(ResourceUtils.getURL("classpath:").getPath());
            if (!path.exists()) path = new File("");
            File filePath = new File(path, "static/images/upload/circle");
            return filePath.getAbsolutePath();
        } catch (FileNotFoundException e) {
            throw new RuntimeException("无法获取上传路径", e);
        }
    }

    public static String getAttachmentFilePath() {
        try {
            File path = new File(ResourceUtils.getURL("classpath:").getPath());
            if (!path.exists()) path = new File("");
            File filePath = new File(path, "static/attachments/upload/circle");
            return filePath.getAbsolutePath();
        } catch (FileNotFoundException e) {
            throw new RuntimeException("无法获取附件路径", e);
        }
    }

    // 新增方法：获取文件大小
    public static long getFileSize(byte[] file) {
        return file.length;
    }
}